const _ = require('lodash');
export const LOGIC = {
  AND: 'and',
  OR: 'or'
};
export const OPERATOR = {
  EQUAL: '=',
  GREATER: '>',
  LESS: '<',
  GREATEREQUAL: '>=',
  LESSEQUAL: '<=',
  STARTSWITH: 'startsWith',
  ENDWITH: 'endWith',
  CONTAINS: 'contains'
};
export const FIELDTYPE = {
  INT: 'int',
  FLOAT: 'float',
  DOUBLE: 'double',
  DATE: 'date',
  STRING: 'string'
};
// 是否时whereSql关键字
const isKeyword = (ch) => {
  return _.some(LOGIC, v => v === ch) ||
    _.some(OPERATOR, v => v === ch) ||
    _.includes(['(', ')'], ch);
};
/**
 * 将whereSql分解成由单词和关键字组成的数组
 * @param {string} whereSql
 */
export const splitWhereSql = (whereSql) => {
  let isQuoteStart = false;
  const words = [];
  let tempWord = '';
  _.each(whereSql, ch => {
    if (ch === '"' || ch === "'") {
      isQuoteStart = !isQuoteStart;
    }
    if (ch === ' ') {
      if (!isQuoteStart) {
        tempWord && words.push(tempWord);
        tempWord = '';
        return;
      }
    }
    if (isKeyword(ch)) {
      if (!isQuoteStart) {
        tempWord && words.push(tempWord);
        words.push(ch);
        tempWord = '';
        return;
      }
    }
    tempWord += ch;
  });
  return words;
};

export const convertToFilter = (whereSqlWords) => {
  const filters = [];
  let tempFilter = null;
  let logic = null;
  const props = ['field', 'operator', 'value'];
  let propIndex = 0;
  let curParent = null;
  _.each(whereSqlWords, (word, index) => {
    if (_.some(LOGIC, v => v === word)) {
      logic = word;
      propIndex = 0;
      if (whereSqlWords[index + 1] !== '(') {
        curParent = _.find(filters, p => p._id === curParent._pid);
      }
    }
    if (word === ')') {
      curParent = _.find(filters, p => p._id === curParent._pid);
      propIndex = 0;
    }
    if (word === '(' || (_.some(LOGIC, v => v === word) && whereSqlWords[index + 1] !== '(')) {
      tempFilter = new Filter();
      tempFilter._id = _.uniqueId('filter_');
      if (curParent) {
        tempFilter._pid = curParent._id;
      }
      curParent = tempFilter;
      if (_.some(LOGIC, v => v === word)) {
        logic = word;
      }
      if (logic) {
        tempFilter.logic = logic;
        logic = null;
      }
      filters.push(tempFilter);
      propIndex = 0;
    }

    if (tempFilter && ['(', ')', 'and', 'or'].indexOf(word) === -1) {
      if (propIndex === 2) {
        word = JSON.parse(word);
      }
      props[propIndex] && (tempFilter[props[propIndex++]] = word);
    }
  });

  _.each(filters, filter => {
    filter.children = _.filter(filters, c => c._pid === filter._id);
  });
  return _.filter(filters, f => !f._pid);
};

/**
 * 针对高级查询封装的查询类
 * 相当于与后端一种高级查询协议
 */
export default class Filter {
  field = null
  logic = LOGIC.AND// 表示与前一个的逻辑关系
  value = null
  fieldType = null
  operator = null
  children = []
  constructor(field, value, operator, fieldType = null) {
    this.field = field;
    this.value = value;
    this.operator = operator;
    this.fieldType = fieldType;
  }
  addChild(filter) {
    this.children.push(filter);
  }
  /**
   *根据字符串转换成filter对象
   * @param {string} whereSql 过滤sql
   */
  static fromWhereSql(whereSql) {
    const words = splitWhereSql(whereSql);
    return convertToFilter(words);
  }
}
